//
//  JSMStrategyContinentDetailViewController.m
//  TakeYouTourTogether
//
//  Created by king on 15/12/6.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMStrategyContinentDetailViewController.h"
#import "JSMStrategyContinentDetailTableViewCell.h"
#import "JSMStrategyContinentAllViewController.h"
#import "JSMStrategyContinentDetailModel.h"
#import "JSMStrategyContinentAllModel.h"

#import "JSMStrategyContinentDetailHotModel.h"

@interface JSMStrategyContinentDetailViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property(nonatomic, strong) BaseTableView *myTableView;
@property(nonatomic, strong) BaseImageView *myImageView;
// 毛玻璃效果
@property(nonatomic, strong) UIVisualEffectView *effectView;
@property(nonatomic, strong) NSArray *dataArray;
@property(nonatomic, strong) NSArray *dataHotPlaceArray;
@property(nonatomic, strong) NSString *strImageURL;
@property(nonatomic, strong) UIView *errorView;

@end

@implementation JSMStrategyContinentDetailViewController
#pragma mark - Metod viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = COLOR(225, 214, 184, 1.0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self requestDataFromNetWorking];
}
- (BaseTableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        _myTableView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_myTableView];
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        _myTableView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
    }
    return _myTableView;
}
- (BaseImageView *)myImageView {
    if (!_myImageView) {
        _myImageView = [[BaseImageView alloc] init];
        _myImageView.clipsToBounds = YES;
        _myImageView.frame = CGRectMake(0, -100, SCREEN_WIDTH, 100);
        [self.myTableView addSubview:_myImageView];
        _myImageView.contentMode = UIViewContentModeScaleAspectFill;
        // 毛玻璃效果
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        self.effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
        self.effectView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);
        self.effectView.alpha = 0.6;
        // 添加子视图
        //    [self.view addSubview:effectView];
        [_myImageView addSubview:self.effectView];
    }
    return _myImageView;
}
#pragma mark - Method 设置拉伸效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat yOffset = scrollView.contentOffset.y;
//    NSLog(@"%g", yOffset);
    if (yOffset < 0) {
        self.myImageView.frame = CGRectMake(0, yOffset, SCREEN_WIDTH, -yOffset);
        self.effectView.alpha = 0.6+yOffset/1000*2;
        NSLog(@"%f", 0.6+yOffset/1000*2);
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *resue = @"resue";
    JSMStrategyContinentDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];
    if (!cell) {
        cell = [[JSMStrategyContinentDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resue];
    }
    JSMStrategyContinentDetailModel *temp = self.dataArray[indexPath.row];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:temp.cover_route_map_cover] placeholderImage:[UIImage imageNamed:PLACE_HOLDER_IMAGE]];
    cell.myLabel.text = temp.name;
    cell.labelButton.text = @"全部推荐地点";
    cell.dataAllArray = self.dataHotPlaceArray;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_WIDTH/2+40;
}
#pragma mark - Method 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"我被点击了");
    JSMStrategyContinentAllViewController *controller = [[JSMStrategyContinentAllViewController alloc] init];
    controller.tempModel = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - Method requestDataFromNetWorking 网络请求数据
- (void)requestDataFromNetWorking {
        NSString *strURL = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/1/%@/", self.tempModel.vid];
        NSLog(@"%@", strURL);
    [AFNetworkingTool getUrl:strURL hudSuperView:self.view result:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSLog(@"%@", dic[@"hottest_sites"]);
        self.dataArray = [JSMStrategyContinentDetailModel baseModelByArr:dic[@"hottest_sites"]];
        self.dataHotPlaceArray = [JSMStrategyContinentDetailHotModel baseModelByArr:dic[@"hottest_places"]];
        self.strImageURL = dic[@"share_args"][@"default"][@"shr_image"];
        [self.myImageView sd_setImageWithURL:[NSURL URLWithString:self.strImageURL] placeholderImage:[UIImage imageNamed:PLACE_HOLDER_IMAGE]];
        [self.errorView removeFromSuperview];
        [self.myTableView reloadData];
        
    } failure:^(NSError *error){
        self.errorView = [[UIView alloc] init];
        self.errorView.backgroundColor = [UIColor clearColor];
        self.errorView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        UIImageView *myImageView = [[UIImageView alloc] init];
        myImageView.frame = CGRectMake(50, 50, SCREEN_WIDTH-100, SCREEN_HEIGHT/2-100);
        myImageView.image = [UIImage imageNamed:@"iconfont-no-wifi-2.png"];
        [self.errorView addSubview:myImageView];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"重新加载" forState:UIControlStateNormal];
        button.frame = CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2+20, 100, 40);
        [button setBackgroundColor:[UIColor blueColor]];
        [button addTarget:self action:@selector(refreshingData:) forControlEvents:UIControlEventTouchUpInside];
        [self.errorView addSubview:button];
        [self.view addSubview:self.errorView];
    }];
}
#pragma mark - Method
- (void)refreshingData:(UIButton *)button {
    [self requestDataFromNetWorking];
}
#pragma mark - Method didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
